package dao;

import entity.AClass;
import entity.PageBean;
import entity.Student;
import util.DruidUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ClassDaoImpl implements ClassDao {
    @Override
    public List<AClass> classShow(PageBean pageBean) {
        List<AClass> classList = null;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            // 1.获取Connection连接对象
            connection = DruidUtils.getConnection();
            // 2.使用StringBuilder类型的对象来描述SQL语句的目的在于后续可以发生改变
            StringBuilder stringBuilder = new StringBuilder("select * from t_class");
            // 3.当输入分页的需求时进行分页查询，于是拼接SQL语句
            if (pageBean != null) {
                stringBuilder.append(" limit " + pageBean.getStart() + "," + pageBean.getRow());
            }
            // 4.获取PreparedStatement并执行上述SQL语句
            preparedStatement = connection.prepareStatement(stringBuilder.toString());
            resultSet = preparedStatement.executeQuery();
            // 5.将结果集中的数据取出并放到List集合中返回
            classList = new ArrayList<>();
            while (resultSet.next()) {
                AClass aClass = new AClass(resultSet.getString("className"),
                        resultSet.getString("Grade"),
                        resultSet.getString("teacher"),
                        resultSet.getString("kouhao"),
                        resultSet.getString("capacity"));
                classList.add(aClass);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close(connection, preparedStatement, resultSet);
        }
        return classList;
    }

    @Override
    public int classAdd(AClass aClass) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            // 1.获取Connection连接对象
            connection = DruidUtils.getConnection();
            // 2.准备插入数据的SQL语句
            String sql = "insert into t_class values(?,?,?,?,?)";
            // 3.获取PreparedStatement并执行SQL语句
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1, aClass.getClassName());
            preparedStatement.setString(2, aClass.getGrade());
            preparedStatement.setString(3, aClass.getTeacher());
            preparedStatement.setString(4, aClass.getKouHao());
            preparedStatement.setString(5, aClass.getCapacity());
            System.out.println("将参数传递给了prestatement");
            int i = preparedStatement.executeUpdate();
            System.out.println(i);
            return i ;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close(connection, preparedStatement);
        }
        return 0; // 表示添加失败
    }

    @Override
    public int classDelete(String[] className) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            // 1.获取Connection连接对象
            connection = DruidUtils.getConnection();
            // 2.准备插入数据的SQL语句
            // 2.1 拼接一下所有的id
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < className.length; i++) {
                if (i == className.length-1) {
                    sb.append(className[i]);
                } else {
                    sb.append(className[i]).append(",");
                }
            }
            // System.out.println("sb = " + sb);
            // 2.2 拼接sql语句
            String sql = "delete from t_class where className in(" + sb + ")";
            // 3.获取PreparedStatement并执行SQL语句
            preparedStatement = connection.prepareStatement(sql);
            System.out.println("...................");
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close(connection, preparedStatement);
        }
        return 0; // 表示删除失败
    }

    @Override
    public int classModify(AClass aClass) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            // 1.获取Connection连接对象
            connection = DruidUtils.getConnection();
            // 2.准备插入数据的SQL语句
            String sql = "update t_class set  Grade=?, teacher=?, kouhao=?, capacity=? where className=?";
            // 3.获取PreparedStatement并执行SQL语句
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, aClass.getGrade());
            preparedStatement.setString(2, aClass.getTeacher());
            preparedStatement.setString(3, aClass.getKouHao());
            preparedStatement.setString(4, aClass.getCapacity());
            preparedStatement.setString(5, aClass.getClassName());
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close(connection, preparedStatement);
        }
        return 0; // 表示修改失败
    }

    @Override
    public AClass classFind(String className) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        AClass aClass = null;
        try {
            // 1.获取Connection连接对象
            connection = DruidUtils.getConnection();
            // 2.准备插入数据的SQL语句
            String sql = "select * from t_class where className=?";
            // 3.获取PreparedStatement并执行SQL语句
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, className);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                aClass = new AClass(resultSet.getString("className"),resultSet.getString("Grade"),resultSet.getString("teacher"),
                        resultSet.getString("kouhao"),resultSet.getString("capacity"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close(connection, preparedStatement, resultSet);
        }
        return aClass;
    }
}
